#include "../include/pnservice.h"

using namespace pnNetlib;

pnService* pnService::g_pInstance = 0;

/***************************************************************************************************************************/
pnService::pnService()
{
	if(g_pInstance == 0)
	{
		g_pInstance = this;
		m_updateService = 0;
		m_isInit = false;
	}
}

/***************************************************************************************************************************/
pnService::~pnService()
{
	if(g_pInstance != 0)
	{
		m_service.stop();
		if(m_updateService != 0)
		{
			delete m_updateService;
		    m_updateService = 0;
		}

		g_pInstance = 0;
		m_isInit = false;
	}
}

/***************************************************************************************************************************/
void
pnService::Create()
{
	if(g_pInstance == 0)
		new pnService();
}

/***************************************************************************************************************************/
void
pnService::Destroy()
{
	if(g_pInstance != 0)
		delete g_pInstance;
}

/***************************************************************************************************************************/
bool
pnService::init(int thread_num, int max_session, long buffer_length)
{
	if(m_isInit == false)
	{
		if(m_actor.init(m_service, max_session, buffer_length) == true)
		{
			m_updateService = new pnUpdateService(m_service, thread_num);
			m_isInit = true;
			return true;
		}
	}

	return false;
}

/***************************************************************************************************************************/
void
pnService::start()
{
	if(m_isInit == true)
	{
		m_updateService->start();
	}
}

/***************************************************************************************************************************/
void
pnService::terminate()
{
	if(m_isInit == true)
	{
		m_service.stop();
		m_updateService->terminate();
        m_actor.close_listen();
        m_actor.close_all_session();
	}
}